
//
//  StatueViewModel.swift
//  swiftImitationWeibo
//
//  Created by 杨果果 on 16/7/13.
//  Copyright © 2016年 yang. All rights reserved.
//

import UIKit

class StatueViewModel: NSObject {

    //转发数显示内容
    var retweetStr: String?
    //评论数
    var commentStr: String?
    //赞
    var unlikeStr:String?
    //处理转发微博内容拼接"@xx:xxxx"
    var retweetContent: String?
    
    //会员等级
    var mbrandImage: UIImage?
    //来源内容
    var sourceContent: String?
    //认证类型等级图片
    var verifiedTypeImage: UIImage?
    
    //持有微博模型   statues
    var statues: Statues?
    init(statues: Statues) {
        super.init()
        self.statues = statues
        retweetStr = handleCount(statues.reposts_count, defaultTitle: "转发")
        commentStr = handleCount(statues.comments_count, defaultTitle: "评论")
        unlikeStr = handleCount(statues.attitudes_count, defaultTitle: "赞")
        
        handleVerifiedTypeImage(statues.user?.verified_type ?? 0)
        handleSourceContent(statues.source ?? "")
        handleRetweetContent(statues)
        handleMBrankImage(statues.user?.mbrank ?? 0)
        
        
    }
    //认证图片
    private func handleVerifiedTypeImage(verifiedType: Int) {
        switch verifiedType {
        case 0: verifiedTypeImage = UIImage(named: "avatar_vip")
        case 2,3,5: verifiedTypeImage = UIImage(named: "avatar_enterprise_vip")
        case 220: verifiedTypeImage = UIImage(named: "avatar_grassroot")
        default : verifiedTypeImage = nil
        }
    }
    //来源字符串
    private func handleSourceContent(source: String) {
        guard let range1 = source.rangeOfString("\">"), let range2 = source.rangeOfString("</") else{
            return
        }
        //起始位置
        let startIndex = range1.endIndex
        //结束位置
        let endIndex = range2.startIndex
        //截取
        sourceContent = "来自 " + source.substringWithRange(startIndex ..< endIndex)
        
    }
    private func handleMBrankImage(mbrank: Int) {
        if mbrank >= 1 && mbrank <= 6 {
            let image = UIImage(named: "common_icon_membership_level\(mbrank)")
            mbrandImage = image
        }
    }
    
    
    
    //转发内容
    private func handleRetweetContent(statues: Statues) {
        if statues.retweeted_status != nil {
            //微博名字和微博内容
            guard let name = statues.retweeted_status?.user?.screen_name, let text = statues.retweeted_status?.text else {
                return
            }
            retweetContent = "@\(name):\(text)"
            
        }
        
    }
    //转发数
    private func handleCount(count: Int, defaultTitle: String) -> String {
        if count > 0 {
            if count >= 10000{
                let result = Float(count/100)/10
                var resultStr = "\(result)万"
                if resultStr.containsString(".0") {
                resultStr = resultStr.stringByReplacingOccurrencesOfString(".0", withString: "")
                
                }
                return resultStr
            } else {
                return "\(count)"
            }
        }else{
            return defaultTitle
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
}
